



//
//  HomePageViewController.m
//  广告页
//
//  Created by YYY on 2017/2/17.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "HomePageViewController.h"

#import "WebViewController.h"

#import "RadarViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "LZAudioTool.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

@interface HomePageViewController ()

@property (nonatomic, strong) UIImageView *upImage;       //背景图片

@property (nonatomic, strong) UIImageView *downImage;     //上一半手 的图片

@property (nonatomic, strong) UIView *upView;             //下一半手

@property (nonatomic, strong) UIView *downView;           //下一半手 的图片

@end

@implementation HomePageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"首页";
    
    [self uiConfig];
 
    //[self becomeFirstResponder];
    /*
    //UIButton宽高自适应
    UIButton *radarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[radarBtn setImage:[UIImage imageNamed:@"location"] forState:UIControlStateNormal];
    radarBtn.frame = CGRectMake(0, 0, 100,50);
   
    [radarBtn setTitle:@"中华人民共和国万岁" forState:UIControlStateNormal];
    radarBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    radarBtn.titleLabel.textColor = [UIColor redColor];
    [radarBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [radarBtn addTarget:self action:@selector(radarAction:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat width = [radarBtn.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, 50) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size.width;
    radarBtn.frame = CGRectMake(0, 0, width+10, 50);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:radarBtn];
    self.view.backgroundColor = [UIColor whiteColor];
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observeAdPage:) name:@"PushAdPage" object:nil];
    // Do any additional setup after loading the view.
}

//雷达
-(void)radarAction
{
    RadarViewController *radarVC = [[RadarViewController alloc]init];
    [self.navigationController pushViewController:radarVC animated:YES];
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}


//跳转广告页
-(void)observeAdPage:(NSNotification *)notification
{
    WebViewController *webViewVC = [[WebViewController alloc]init];
    webViewVC.title = @"广告页";
    webViewVC.webUrlStr = notification.object;
    [self.navigationController pushViewController:webViewVC animated:YES];
}
- (void)uiConfig
{
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, kHeight / 4, kWidth, kHeight / 2)];
    image.image = [UIImage imageNamed:@"ShakeHideImg_women"];
    [self.view addSubview:image];
    
    _upView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight / 2)];
    _upView.backgroundColor = [UIColor blackColor];
    
    _upImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, kHeight / 4, kWidth - 100, kHeight / 4)];
    _upImage.image = [UIImage imageNamed:@"Shake_Logo_Up"];
    
    [_upView addSubview:_upImage];
    [self.view addSubview:_upView];
    
    
    
    _downView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight / 2, kWidth, kHeight / 2)];
    _downView.backgroundColor = [UIColor blackColor];
    
    _downImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, 0, kWidth - 100, kHeight / 4)];
    _downImage.image = [UIImage imageNamed:@"Down"];
    
    [_downView addSubview:_downImage];
    [self.view addSubview:_downView];
}

#pragma mark 摇一摇代理
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"检测到摇动");
    //开始摇晃 设置动画
    [UIView animateWithDuration:1 animations:^{
        
        _upView.frame = CGRectMake(0, -(kHeight / 4), kWidth, kHeight / 2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            _upView.frame = CGRectMake(0, 0, kWidth, kHeight / 2);
            
        }];
    }];
    
    [UIView animateWithDuration:1 animations:^{
        
        _downView.frame = CGRectMake(0, kHeight / 2 + kHeight / 4, kWidth, kHeight / 2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            
            _downView.frame = CGRectMake(0, kHeight / 2, kWidth, kHeight / 2);
            
        }];
        
    }];
    //播放摇晃声音
    [LZAudioTool playMusic:@"1.mp3"];
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇动取消");
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇动结束");
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        NSLog(@"跳转界面");
        [self radarAction];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
